home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13587 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  97 lines

  1. Path: news.microsoft.com!news
  2. From: a-cnadc@microsoft.com (Dann Corbit)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Recursion
  5. Date: 6 Apr 1996 03:06:40 GMT
  6. Organization: Microsoft Corporation
  7. Message-ID: <4k4n40$tbi@news.microsoft.com>
  8. References: <31624BC2.70D2@sooner.net> <828548265snz@genesis.demon.co.uk> <4k10q0$m5d@linet06.li.net>
  9. NNTP-Posting-Host: 157.57.171.202
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.93.14
  12.  
  13. In article <4k10q0$m5d@linet06.li.net>, jeremy@newshost.li.net says...
  14. >
  15. >Lawrence Kirby (fred@genesis.demon.co.uk) wrote:
  16. >
  17. >: I think you've picked a bad example. You really need an extra argument in
  18. >: this case to pass on a running accumulator. A more suitable problem would
  19. >: be to write a recursive function that is passed an integer (unsigned is
  20. >: easiest) and writes the character representation to stdout.
  21. >Absolutely WRONG.  The whole point of recursion is that you do not need a 
  22. >running accumulator.  When written correctly, the return value of each 
  23. >recursive loop will be the correct accumulated value.  ANyhow, here is a 
  24. >better version of what I already posted...
  25. >
  26. >#include <stdio.h>
  27. >#include <stdlib.h>
  28. >#include <string.h>
  29. >#include <math.h>
  30. >
  31. >int convert(char *string)
  32. > {
  33. >  int value;
  34. >  int len = strlen(string);
  35. >
  36. >  if (string[0] == '\0')
  37. >    return(0);
  38. >  value = convert(string+1);
  39. >  value += ((string[0] - '0') * pow(10,len-1));
  40. >  return(value);
  41. > }
  42. >
  43. >void main()
  44. > {
  45. >  char *string = "1234";
  46. >
  47. >  printf("%d\n",convert(string));
  48. > }
  49.  
  50. I have to agree, with Mr. Kirby that recursion is a most
  51. inelegant way to do this.  Here is another ugly recursive
  52. solution.  The elegant way is atoi().  Iterative is next
  53. best.  Recursive is downright silly.  But we all have our
  54. silly moments.
  55.  
  56. #include <stdio.h>
  57. #include <ctype.h>
  58. #include <string.h>
  59.  
  60. /* Notes: 
  61.  * 1. Clobbers input string.  
  62.  * 2. No error checking 
  63.  * 3. Plain old double-mugged ugly.
  64.  */
  65. int convert( char *string )
  66. {
  67.    int value = 0;
  68.    /* Is there stuff in the string? */
  69.    if ( *string )
  70.    {  /* Reverse it to deal with smallest digit first */
  71.       strrev( string );
  72.       if ( isdigit( *string ) ) /* Make sure digit is from 0 to 9 */
  73.       {
  74.          value = *string - '0'; /* calculate the value */
  75.       }
  76.       string++; /* Go to the next character */
  77.       if ( *string ) /* if we still have more stuff, recurse */
  78.       {
  79.          strrev( string ); /* Reverse (n-1) chars back */
  80.          value += 10*convert( string );
  81.       }
  82.    }
  83.    return ( value );
  84. }
  85.  
  86. void main( )
  87. {
  88.    char *string = "1234";
  89.  
  90.    printf( "%d\n", convert( string ) );
  91. }
  92. -- 
  93. The opinions expressed in this message are my own personal views
  94. and do not reflect the official views of Microsoft Corporation.
  95. In fact, I'm just a subcontractor, not an employee, so pull in your claws.
  96.  
  97.